﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CuongHanh.Context;
using CuongHanh.Models;
using CuongHanh.Security;
using CuongHanh.Service;
using Ext.Net.MVC;

#endregion

namespace CuongHanh.Controllers.BO
{
    [CustomAuthorize(Key = "Management", Description = "Manage all apps")]
    public class AMyStyleController : ABaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ShowAdd(string containerId)
        {
            return ShowWindow("Add",
                    containerId,
                    new MyStyle(),
                    550,
                    265);
        }

        [HttpPost]
        public ActionResult Add(MyStyle model)
        {
            MyStyle newModel = new MyStyle(model);
            if(Request.Files.Count > 0
               && Request.Files[0].ContentLength > 0)
            {
                byte[] imgData = new byte[Request.Files[0].ContentLength];
                Request.Files[0].InputStream.Read(imgData,
                        0,
                        Request.Files[0].ContentLength);
                string fileName = WriteByteToFileForGem(imgData,
                        Request.Files[0].InputStream);
                newModel.Image = fileName;
            }
            newModel.Description = string.Empty;
            newModel.DescriptionEn = string.Empty;
            UpdateResult result = MyStyleService.Insert(newModel);
            ShowWindow("wdModify",
                    false);
            ReloadStore("storeGvData");
            ShowNotification(result);
            return this.Direct();
        }

        public ActionResult Command(string containerId,
                string command,
                int id,
                int? materialId,
                int? stoneId,
                int? parentId)
        {
            if(command == "Delete")
            {
                UpdateResult result = MyStyleService.Delete(id);
                ReloadStore("storeGvData");
                ShowNotification(result);
            }
            if(command == "Edit")
            {
                MyStyle model = MyStyleService.Get(id);
                return ShowWindow("Edit",
                        containerId,
                        model,
                        550,
                        265);
            }

            if (command == "Description")
            {
                MyStyle model = MyStyleService.Get(id);
                model.Description_e = model.Description;
                model.Description = string.Empty;
                model.DescriptionEn_e = model.DescriptionEn;
                model.DescriptionEn = string.Empty;
                
                return ShowWindow("Description",
                        containerId,
                        model,
                        800,
                        500);
            }
            if(command == "Material")
            {
                return ShowWindow("StyleMaterial",
                        containerId,
                        id,
                        800,
                        400);
            }
            if(command == "EditStyleMaterial")
            {
                StyleMaterial model = MyStyleService.GetStyleMaterial(id,
                        materialId.HasValue
                                ? materialId.Value
                                : 0);
                return ShowWindow("EditStyleMaterial",
                        containerId,
                        model);
            }
            if(command == "DeleteMaterial")
            {
                UpdateResult result = MyStyleService.DeleteMaterial(id,
                        materialId);
                ReloadStore("storeGvChildData");
                ShowNotification(result);
            }

            #region styleStone

            if(command == "StyleStone")
            {
                return ShowWindow("StyleStone",
                        containerId,
                        id,
                        800,
                        400);
            }
            if(command == "EditStyleStone")
            {
                StyleStone model = MyStyleService.GetStyleStone(id,
                        stoneId.HasValue
                                ? stoneId.Value
                                : 0);
                return ShowWindow("EditStyleStone",
                        containerId,
                        model);
            }
            if(command == "DeleteStyleStone")
            {
                UpdateResult result = MyStyleService.DeleteStyleStone(id,
                        stoneId);
                ReloadStore("storeGvChildData");
                ShowNotification(result);
            }

            #endregion

            #region Parent Material

            if(command == "ParentMaterial")
            {
                return ShowWindow("ParentMaterial",
                        containerId,
                        new Tuple<int, int?>(id,
                                parentId),
                        800,
                        400);
            }

            #endregion

            return this.Direct();
        }

        [HttpPost]
        public ActionResult Edit(MyStyle model)
        {
            if(Request.Files.Count > 0
               && Request.Files[0].ContentLength > 0)
            {
                byte[] imgData = new byte[Request.Files[0].ContentLength];
                Request.Files[0].InputStream.Read(imgData,
                        0,
                        Request.Files[0].ContentLength);
                string fileName = WriteByteToFileForGem(imgData,
                        Request.Files[0].InputStream);
                model.Image = fileName;
            }
            UpdateResult result = MyStyleService.Update(model);
            ShowWindow("wdModify",
                    false);
            ReloadStore("storeGvData");
            ShowNotification(result);
            return this.Direct();
        }



        public ActionResult Description(int id)
        {
            return View(MyStyleService.Get(id));
        }

        [HttpPost]
        public ActionResult Description(MyStyle model)
        {

            if(string.IsNullOrWhiteSpace(model.Description))
                model.Description = string.Empty;
            if (string.IsNullOrWhiteSpace(model.DescriptionEn))
                model.DescriptionEn = string.Empty;
            UpdateResult result = MyStyleService.UpdateDescription(model);
            ShowWindow("wdModify",
                    false);
            ReloadStore("storeGvData");
            ShowNotification(result);
            return this.Direct();
        }


        public ActionResult GetDatas(int start,
                int limit,
                string query)
        {
            return Content(MyStyleService.Gets(start,
                    limit,
                    query),
                    "json");
        }

        private string SaveFile(HttpRequestBase request)
        {
            if(request.Files.Count <= 0
               || request.Files[0] == null
               || request.Files[0].ContentLength <= 0)
                return string.Empty;
            byte[] imgData = new byte[request.Files[0].ContentLength];
            request.Files[0].InputStream.Read(imgData,
                    0,
                    request.Files[0].ContentLength);
            return WriteByteToFileForGem(imgData,
                    request.Files[0].InputStream);
        }

        #region Style Material

        public ActionResult StyleMaterial()
        {
            return View();
        }

        public ActionResult GetStyleMaterials(int styleId)
        {
            return Content(MyStyleService.GetStyleMaterials(styleId),
                    "json");
        }

        public ActionResult ShowAddStyleMaterial(string containerId,
                int styleId)
        {
            var temp = CacheList<StyleStone>.Singleton.GetCache(styleId);
            int no = temp.Any()
                    ? temp.Max(c => c.No)
                    : 0;
            return ShowWindow("AddStyleMaterial",
                    containerId,
                    new StyleMaterial()
                    {
                            StyleId = styleId,
                            No = ++no
                    });
        }

        [HttpPost]
        public ActionResult AddStyleMaterial(StyleMaterial model)
        {
            StyleMaterial newModel = new StyleMaterial(model);
            if(Request.Files.Count > 0
               && Request.Files[0].ContentLength > 0)
            {
                byte[] imgData = new byte[Request.Files[0].ContentLength];
                Request.Files[0].InputStream.Read(imgData,
                        0,
                        Request.Files[0].ContentLength);
                string fileName = WriteByteToFileForGem(imgData,
                        Request.Files[0].InputStream);
                newModel.Image = fileName;
            }

            UpdateResult result = MyStyleService.InsertStyleMaterial(newModel);
            ShowWindow("wdModifyChild",
                    false);
            ReloadStore("storeGvChildData");
            ShowNotification(result);
            return this.Direct();
        }

        [HttpPost]
        public ActionResult EditStyleMaterial(StyleMaterial model)
        {
            StyleMaterial newModel = new StyleMaterial(model);
            if(Request.Files.Count > 0
               && Request.Files[0].ContentLength > 0)
            {
                byte[] imgData = new byte[Request.Files[0].ContentLength];
                Request.Files[0].InputStream.Read(imgData,
                        0,
                        Request.Files[0].ContentLength);
                string fileName = WriteByteToFileForGem(imgData,
                        Request.Files[0].InputStream);
                newModel.Image = fileName;
                //todo delete old file if exits
            }

            UpdateResult result = MyStyleService.UpdateStyleMaterial(newModel);
            ShowWindow("wdModifyChild",
                    false);
            ReloadStore("storeGvChildData");
            ShowNotification(result);
            return this.Direct();
        }

        public ActionResult GetMaterialsCombo(int styleId,
                int? materialId)
        {
            return Content(MaterialService.GetsCombo(styleId,
                    materialId));
        }

        #endregion

        #region Style Stone

        public ActionResult StyleStone()
        {
            return View();
        }

        public ActionResult GetStyleStones(int styleId)
        {
            return Content(MyStyleService.GetStyleStones(styleId),
                    "json");
        }

        public ActionResult ShowAddStyleStone(string containerId,
                int styleId)
        {

            var temp = CacheList<StyleStone>.Singleton.GetCache(styleId);
            int no = temp.Any()
                    ? temp.Max(c => c.No)
                    : 0;
            return ShowWindow("AddStyleStone",
                    containerId,
                    new StyleStone()
                    {
                            StyleId = styleId,
                            No=++no
                    });
        }

        [HttpPost]
        public ActionResult AddStyleStone(StyleStone model)
        {
            StyleStone newModel = new StyleStone(model);
            if(Request.Files.Count > 0
               && Request.Files[0].ContentLength > 0)
            {
                byte[] imgData = new byte[Request.Files[0].ContentLength];
                Request.Files[0].InputStream.Read(imgData,
                        0,
                        Request.Files[0].ContentLength);
                string fileName = WriteByteToFileForGem(imgData,
                        Request.Files[0].InputStream);
                newModel.Image = fileName;
            }

            UpdateResult result = MyStyleService.InsertStyleStone(newModel);
            ShowWindow("wdModifyChild",
                    false);
            ReloadStore("storeGvChildData");
            ShowNotification(result);
            return this.Direct();
        }

        [HttpPost]
        public ActionResult EditStyleStone(StyleStone model)
        {
            StyleStone newModel = new StyleStone(model);
            if(Request.Files.Count > 0
               && Request.Files[0].ContentLength > 0)
            {
                byte[] imgData = new byte[Request.Files[0].ContentLength];
                Request.Files[0].InputStream.Read(imgData,
                        0,
                        Request.Files[0].ContentLength);
                string fileName = WriteByteToFileForGem(imgData,
                        Request.Files[0].InputStream);
                newModel.Image = fileName;
                //todo delete old file if exits
            }

            UpdateResult result = MyStyleService.UpdateStyleStone(newModel);
            ShowWindow("wdModifyChild",
                    false);
            ReloadStore("storeGvChildData");
            ShowNotification(result);
            return this.Direct();
        }

        public ActionResult GetStonesCombo(int styleId,
                int? stoneId)
        {
            return Content(MyStoneService.GetsCombo(styleId,
                    stoneId));
        }

        #endregion

        #region Style Material

        public ActionResult ParentMaterial()
        {
            return View();
        }

        public ActionResult GetParentMaterials(int styleId)
        {
            return Content(MyStyleService.GetParentMaterials(styleId),
                    "json");
        }

        public ActionResult ShowAddParentMaterial(string containerId,
                int styleId,
                int parentStyleId)
        {
            return ShowWindow("AddParentMaterial",
                    containerId,
                    new ParentMaterial()
                    {
                            ChildStyleId = styleId,
                            ParentStyleId = parentStyleId
                    },
                    550,
                    180);
        }

        public ActionResult AddParentMaterial(string containerId,
                int styleId)
        {
            return View(new ParentMaterial()
                        {
                                ChildStyleId = styleId
                        });
        }

        [HttpPost]
        public ActionResult AddParentMaterial(ParentMaterial model)
        {
            ParentMaterial newModel = new ParentMaterial(model);
            newModel.Image = SaveFile(Request);

            UpdateResult result = MyStyleService.InsertParentMaterial(newModel);
            ShowWindow("wdModifyChild",
                    false);
            ReloadStore("storeGvChildData");
            ShowNotification(result);
            return this.Direct();
        }

        [HttpPost]
        public ActionResult EditParentMaterial(ParentMaterial model)
        {
            ParentMaterial newModel = new ParentMaterial(model);
            newModel.Image = SaveFile(Request);

            UpdateResult result = MyStyleService.UpdateParentMaterial(newModel);
            ShowWindow("wdModifyChild",
                    false);
            ReloadStore("storeGvChildData");
            ShowNotification(result);
            return this.Direct();
        }

        public ActionResult GetStylesCombo()
        {
            return Content(MyStyleService.GetStylesCombo());
        }

        public ActionResult GetMaterialsOfParentCombo(int styleId,
                int parentStyleId,
                int? parentMaterialId)
        {
            return Content(MyStyleService.GetMaterialsOfParentCombo(styleId,
                    parentStyleId,
                    parentMaterialId));
        }

        public ActionResult ParentMaterialCommand(string containerId,
                string command,
                int childStyleId,
                int parentStyleId,
                int parentMaterialId)
        {
            if(command == "EditParentMaterial")
            {
                ParentMaterial model = MyStyleService.GetParentMaterial(parentStyleId,
                        parentMaterialId,
                        childStyleId);
                return ShowWindow("EditParentMaterial",
                        containerId,
                        model,
                        550,
                        180);
            }
            if(command == "DeleteParentMaterial")
            {
                UpdateResult result = MyStyleService.DeleteParentMaterial(parentStyleId,
                        parentMaterialId,
                        childStyleId);
                ReloadStore("storeGvChildData");
                ShowNotification(result);
            }
            return this.Direct();
        }

        #endregion

        public ActionResult GetStyleTypesCombo()
        {
            return Content(MyStyleService.GetStyleTypesCombo());
        }

    }
}
